package service;

import dao.ImplementUser;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
@WebServlet("/loginServlet2")
public class UserLoginServlet2 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String usename = req.getParameter("usename");
        String password = req.getParameter("password");
        //调用UserDao的login方法
        ImplementUser implement=new ImplementUser();
        boolean login=implement.login(usename,password);
        //登陆失败--转发
        if (!login) {
            //登陆失败
            req.setAttribute("login_fail","用户名或密码错误");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
        else {
            //登录成功
            HttpSession session=req.getSession();
            session.setAttribute("usename",usename);
            resp.sendRedirect(req.getContextPath()+"/allAreas.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
